package com.cosina.game.robodefence.findway;






public class Tile {
	private static final int NULL_CODE = -987;
	public static Tile UN_EXIST_TILE = new Tile(-1, -1, NULL_CODE, null);
	public final int x, y;
	private int code;
	private Chart belongChart;

	public Tile(int x, int y, int code, Chart belongChart) {
		this.code = code;
		this.x = x;
		this.y = y;
		this.belongChart = belongChart;
	}

	public boolean isBlank() {
		return this.code == 0;
	}

	private Tile getNear(Direction direction) {
		if (null == belongChart)
			return UN_EXIST_TILE;

		if (Direction.north == direction)
			return belongChart.get(x, y - 1);
		if (Direction.east == direction)
			return belongChart.get(x + 1, y);
		if (Direction.south == direction)
			return belongChart.get(x, y + 1);
		if (Direction.west == direction)
			return belongChart.get(x - 1, y);
		throw new IllegalArgumentException("which direction? are you crazy!!");
	}

	private boolean toOtherXBlank(Tile other) {
		int min = Math.min(this.x, other.x);
		int max = Math.max(this.x, other.x);
		for (int index = min + 1; index < max; index++) {
			if (this.belongChart.get(index, y).isBlank() == false) {
				return false;
			}
		}
		return true;
	}

	private boolean toOtherYBlank(Tile other) {
		int min = Math.min(this.y, other.y);
		int max = Math.max(this.y, other.y);
		for (int index = min + 1; index < max; index++) {
			if (this.belongChart.get(x, index).isBlank() == false) {
				return false;
			}
		}
		return true;
	}

	public boolean isLink(BlankRoute route) {
		if (this.code == NULL_CODE || route.isInPath(this)){
			return false;
		}
		if (this.isBlank() || this == route.start) {{
				route.addRouteTile(this);
			}

			Tile eastNear = getNear(Direction.east);
			Tile southNear = getNear(Direction.south);
			Tile westNear = getNear(Direction.west);
			Tile northNear = getNear(Direction.north);
			if (eastNear == route.end || westNear == route.end
					|| southNear == route.end || northNear == route.end) {
				return route.addRouteTile(route.end);
			}

			boolean result = false;
		
				if (this.x == route.end.x) {
					if (toOtherYBlank(route.end)) {
						if (this.y > route.end.y) {
							result = northNear.isLink(route);
						} else {
							result = southNear.isLink(route);
						}
					}else{
						result = result ||
						westNear.isLink(route) || eastNear.isLink(route) || southNear.isLink(route) ||northNear.isLink(route);
					}

				} else if (this.y == route.end.y) {
					if (toOtherXBlank(route.end)) {
						if (this.x > route.end.x) {
							result = westNear.isLink(route);
						} else {
							result = eastNear.isLink(route);
						}
					}else{
						result = result ||
						northNear.isLink(route) || southNear.isLink(route) ||eastNear.isLink(route) ||westNear.isLink(route);
					}
				}
			else {
					if (this.x > route.end.x) {
						if (this.y > route.end.y) {
							result = result || westNear.isLink(route)|| northNear.isLink(route) || eastNear.isLink(route) || southNear.isLink(route);
						} else {
							result = result || westNear.isLink(route)|| southNear.isLink(route) || eastNear.isLink(route) || northNear.isLink(route);
						}
					}else{
						if (this.y > route.end.y) {
							result = result || eastNear.isLink(route) || northNear.isLink(route) || westNear.isLink(route)|| southNear.isLink(route) ;
						} else {
							result = result || eastNear.isLink(route) || southNear.isLink(route) || westNear.isLink(route)|| northNear.isLink(route) ;
						}
					}
				}
			if (result == false) {
				route.removeTile(this);
			}
			return result;
		}
		return false;
	}
	
	
	public String toString(){
		return "X:" + x  + " Y:" + y;
	}
}
